package cn.xs.goods.action;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import cn.xs.core.entity.PageResult;
import cn.xs.core.util.IDUtils;
import cn.xs.goods.entity.Category;
import cn.xs.goods.entity.Good;
import cn.xs.goods.service.CategoryService;
import cn.xs.goods.service.GoodService;
import cn.xs.order.entity.Car;
import cn.xs.order.entity.CarItem;
import cn.xs.user.entity.User;

import com.opensymphony.xwork2.ActionSupport;

public class GoodAction extends ActionSupport {

	@Resource
	private CategoryService categoryService;
	@Resource
	private GoodService goodService;
	private List<Category> categoryList;
	private Good good;
	private int number;	//数量
	
	private Category pcategory;	//父分类
	private Category category;	//子分类
	
	private PageResult pageResult;	//分页对象
	private int pageNo;//页号
	private int pageSize;//页大小
	
	private String searchName;
	
	//跳转到衣服分类的商品页面,这个是入口
	public String toClothesUI(){
		if(pcategory!=null && pcategory.getId()!=null){
			categoryList = categoryService.findCategorysByPid(pcategory.getId());
			pageResult = goodService.getPageResult(category.getId(),pageNo,getPageSize());
			
		}
		return "clothesUI";
	}
	//跳转到鞋子分类的商品页面,这个是入口
		public String toShoesUI(){
			if(pcategory!=null && pcategory.getId()!=null){
				categoryList = categoryService.findCategorysByPid(pcategory.getId());
				pageResult = goodService.getPageResult(category.getId(),pageNo,getPageSize());
				
			}
			return "shoesUI";
		}
	//跳转到数码分类的商品页面,这个是入口
	public String toDigitalUI(){
		if(pcategory!=null && pcategory.getId()!=null){
			categoryList = categoryService.findCategorysByPid(pcategory.getId());
			pageResult = goodService.getPageResult(category.getId(),pageNo,getPageSize());
			
		}
		return "digitalUI";
	}
	
	/**
	 * 检索方法
	 * @return
	 */
	public String toSearchUI(){
		if(searchName!=null && !searchName.equals("")){
			pageResult = goodService.getPageResult(pageNo, getPageSize(),searchName);
		}
		return "searchUI";
	}
	//跳转到鞋子详细页面
	public String toShoes(){
		if(pcategory!=null && pcategory.getId()!=null && good!=null){
			categoryList = categoryService.findCategorysByPid(pcategory.getId());
			good = goodService.find(good.getId());
		}
		return "shoes";
	}
	
	//跳转到衣服详细页面
	public String toClothes(){
		if(pcategory!=null && pcategory.getId()!=null && good!=null){
			categoryList = categoryService.findCategorysByPid(pcategory.getId());
			good = goodService.find(good.getId());
		}
		return "clothes";
	}
	
	//跳转到数码详细页面
		public String toDigital(){
			if(pcategory!=null && pcategory.getId()!=null && good!=null){
				categoryList = categoryService.findCategorysByPid(pcategory.getId());
				good = goodService.find(good.getId());
			}
			return "digital";
		}
	/**
	 *	添加购物项,然后转到消息页面
	 *		1.判断有没登录
	 *		2.添加购物项分两步
	 *			2.1 判断当前session中有没购物车(Car),没有就创建,拿到
	 *			2.2 判断本次将要添加的商品是否已经有对应的购物项,没有即创建,有就修改其数量
	 *		3.拿到session中CarNumber(商品数量)和sumPrice(总金额),没有就创建,修改数量  
	 *		4.跳转到msg.jsp去
	 */
	public String addCar(){
		
		if(good!=null && number != 0 ){
			//查询出当前添加的商品
			good = goodService.find(good.getId());
			HttpSession session  = ServletActionContext.getRequest().getSession();
			//拿到当前用户
			User userInfo = (User) session.getAttribute("USER_INFO");
			//判断有没登录
			if(userInfo == null){
				//跳转到登录页面去
				return "loginUI";
			}
			//拿到购物车实例Car
			Car car =(Car) session.getAttribute("Car");
			
			//判断购物车是否存在
			if(car !=null ){
				//先拿到总价和购物车商品数量
				Double sumPrice = car.getSumprice();
				if(sumPrice == null){
					sumPrice = new Double(0);
				}
				Integer carNumber = car.getCarNumber();
				if(carNumber == null){
					carNumber = new Integer(0);
				}
				//遍历判断
				for(CarItem item:car.getItems()){
					Good igood = item.getGood();
					
					//判断商品是否已经存在对应购物项,存在的话就在其基础上更新信息
					if(igood.getId().equals(good.getId())){
						item.setQuantity(item.getQuantity()+number);
						carNumber +=number;
						sumPrice +=number*igood.getPrice();
						car.setCarNumber(carNumber);
						car.setSumprice(sumPrice);
						//跳转到消息页面去
						return "msg";
					}
				}
				
				//新的一种商品添加进购物车
				CarItem item = new CarItem(IDUtils.getRandomID(), good, number);
				car.getItems().add(item);
				carNumber +=number;
				sumPrice +=item.getPrice();
				car.setCarNumber(carNumber);
				car.setSumprice(sumPrice);
				
			}else{
				//购物车是空的,商品第一次添加
				car = new Car();
				//设置购物车的字段属性
				List<CarItem> items = new ArrayList<CarItem>();
				CarItem item = new CarItem(IDUtils.getRandomID(), good, number);
				items.add(item);
				car.setId(IDUtils.getRandomID());
				car.setCarNumber(number);
				car.setSumprice(item.getPrice());
				car.setUser(userInfo);
				car.setItems(items);
				//添加购物车进session
				session.setAttribute("Car", car);
			}
			
		}
		return "msg";
	}

	
	public List<Category> getCategoryList() {
		return categoryList;
	}
	public void setCategoryList(List<Category> categoryList) {
		this.categoryList = categoryList;
	}
	public Category getPcategory() {
		return pcategory;
	}
	public void setPcategory(Category pcategory) {
		this.pcategory = pcategory;
	}
	public Category getCategory() {
		return category;
	}
	public void setCategory(Category category) {
		this.category = category;
	}
	public PageResult getPageResult() {
		return pageResult;
	}
	public void setPageResult(PageResult pageResult) {
		this.pageResult = pageResult;
	}
	public int getPageNo() {
		return pageNo;
	}
	public void setPageNo(int pageNo) {
		this.pageNo = pageNo;
	}
	public int getPageSize() {
		if(pageSize<1) this.pageSize = 8;	//默认一页显示8个商品
		return pageSize;
	}
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
	public Good getGood() {
		return good;
	}
	public void setGood(Good good) {
		this.good = good;
	}
	public int getNumber() {
		return number;
	}
	public void setNumber(int number) {
		this.number = number;
	}
	public String getSearchName() {
		return searchName;
	}
	public void setSearchName(String searchName) {
		this.searchName = searchName;
	}
	
}
